import {
	IWFDynaActionViewAbility,
	WFDynaActionViewActionType,
	IWFDynaActionViewStore,
	IWFDynaActionViewController,
	IWFDynaActionViewControllerParams,
	IParam,
	IWFDynaActionViewModel,
	IEditFormAbility,
	ICtrlActionResult,
} from '@/core/interface';
import { DEViewController } from './de-view-controller';

/**
 * 工作流动态选项操作视图
 *
 * @export
 * @class WFDynaActionViewController
 * @extends {DEViewController<WFDynaActionViewActionType, IWFDynaActionViewStore, IWFDynaActionViewAbility>}
 * @implements {IWFDynaActionViewController<IWFDynaActionViewStore, IWFDynaActionViewAbility>}
 */
export class WFDynaActionViewController
	extends DEViewController<
		WFDynaActionViewActionType,
		IWFDynaActionViewStore,
		IWFDynaActionViewAbility
	>
	implements
		IWFDynaActionViewController<
			IWFDynaActionViewStore,
			IWFDynaActionViewAbility
		>
{
	/**
	 * 工作流动态选项操作视图模型
	 *
	 * @protected
	 * @type {IWFDynaActionViewModel}
	 * @memberof WFDynaActionViewController
	 */
	protected declare readonly model: IWFDynaActionViewModel;

	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {IWFDynaActionViewControllerParams<WFDynaActionViewActionType, IWFDynaActionViewAbility>} params
	 * @memberof WFDynaActionViewController
	 */
	protected processViewInit(
		params: IWFDynaActionViewControllerParams<
			WFDynaActionViewActionType,
			IWFDynaActionViewAbility
		>
	) {
		super.processViewInit(params);
		Object.assign(this.store, { activeForm: {} });
	}

	/**
	 * 获取表单能力
	 *
	 * @protected
	 * @return {*}  {(IEditFormAbility | undefined)}
	 * @memberof WFDynaEditViewController
	 */
	protected getFormAbility(): IEditFormAbility | undefined {
		if (this.store.activeForm) {
			return this.getSubAbility<IEditFormAbility>(this.store.activeForm.name);
		}
		return undefined;
	}

	/**
	 * 视图挂载
	 *
	 * @param {IParam} [opts={}]
	 * @memberof WFDynaActionViewController
	 */
	public viewMounted(opts: IParam = {}) {
		super.viewMounted(opts);
		if (this.model.useDefaultLayout) {
			if (!this.isLoadDefault) {
				this.isLoadDefault = true;
				return;
			}
			this.computeActiveForm();
		} else {
			this.initLayout().then(() => {
				this.computeActiveForm();
			});
		}
	}

	/**
	 * 计算激活表单
	 *
	 * @memberof WFDynaActionViewController
	 */
	private computeActiveForm() {
		const { viewParams } = this.store;
		const { dynamicForm } = this.model;
		let activeForm: any = null;
		if (viewParams.actionForm) {
			activeForm = dynamicForm.find((form: IParam) => {
				return (
					form.name.toLowerCase() ===
					`wfform_${viewParams.actionForm.toLowerCase()}`
				);
			});
		} else {
			activeForm = dynamicForm.find((form: IParam) => {
				return form.name.toLowerCase() === 'form';
			});
		}
		if (activeForm) {
			this.store.activeForm = activeForm;
		}
	}

	/**
	 * 确认
	 *
	 * @memberof WFDynaActionViewController
	 */
	public async confirm() {
		const form = this.getFormAbility();
		if (form) {
			if (this.model.tempMode === 2) {
				form.save().then((response: ICtrlActionResult) => {
					if (response.ok) {
						this.emit('viewDataChange', response.data ? response.data : []);
						this.emit('viewClose', response.data ? response.data : []);
					}
				});
			} else {
				if (await form.validate()) {
					this.emit('viewDataChange', form.getData());
					this.emit('viewClose', form.getData());
				}
			}
		}
	}

	/**
	 * 取消
	 *
	 * @memberof WFDynaActionViewController
	 */
	public cancel() {
		this.emit('viewClose', []);
	}

	/**
	 * 表单加载
	 *
	 * @private
	 * @memberof WFDynaActionViewController
	 */
	private onFormLoad() {
		const form = this.getFormAbility();
		const { entityCodeName } = this.model;
		if (form && entityCodeName) {
			const key = this.store.context[entityCodeName.toLowerCase()];
			if (key) {
				form.load({ [entityCodeName.toLowerCase()]: key });
			} else {
				form.loadDraft({});
			}
		}
	}

	/**
	 * 处理部件初始化
	 *
	 * @param {string} name
	 * @param {IEditFormAbility} ability
	 * @memberof WFDynaActionViewController
	 */
	public handleCtrlInit(name: string, ability: IEditFormAbility) {
		super.handleCtrlInit(name, ability);
		this.onFormLoad();
	}
}
